package fr.toto.settings.defaultPanels;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class JAddTextButton extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Create the panel.
	 */
	public JAddTextButton(final String text, String explication, final JTextField destination) {
		setMinimumSize(new Dimension(100, 30));
		setMaximumSize(new Dimension(32767, 30));
		setBorder(null);
		
		JButton btnNewButton = new JButton(text);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String desttext = destination.getText();
				int pos = destination.getCaretPosition();
				String newtext = desttext.substring(0,pos) + text + desttext.substring(pos);
				destination.setText(newtext);
				destination.setCaretPosition(pos+ text.length());
				destination.requestFocus();
			}
		});
		setLayout(new FormLayout(new ColumnSpec[] {
				ColumnSpec.decode("150px"),
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				RowSpec.decode("35px"),}));
		add(btnNewButton, "1, 1, fill, center");
		
		JLabel lblNewLabel = new JLabel(explication);
		add(lblNewLabel, "3, 1, fill, center");

		this.setFocusable(false);
	}

}
